package de.uni.bremen.estudent.lbs.ui;


import android.app.Dialog;
import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import de.uni.bremen.estudent.lbs.R;
import de.uni.bremen.estudent.lbs.models.LbsOverlayItem;
import de.uni.bremen.estudent.lbs.provider.EstudentLBSContract.Meals;
import de.uni.bremen.estudent.lbs.util.Constants;
import de.uni.bremen.estudent.lbs.util.MensaCursorAdapter;

import java.util.Calendar;


/**
 * Class which represents the Food dialog.
 *
 * @author bahlfeld
 * @version 1.0 16.03.2012
 */
public final class FoodDialog extends Dialog {

    /**
     * The applicationt context.
     */
    private final Context mContext;
    /**
     * Overlayitem.
     */
    private final LbsOverlayItem mItem;
    /**
     * Padding for the textview.
     */
    private final int PADDING = 10;

    public FoodDialog(final Context context, final LbsOverlayItem item) {
        super(context, R.style.FilterDialogTheme);
        this.mContext = context;
        this.mItem = item;

        final String[] projection = new String[] { Meals.MEAL_ID,
                Meals.MEAL_NAME, Meals.MEAL_DESC, Meals.MEAL_STAFF_PRICE,
                Meals.MEAL_STUDENT_PRICE };

        Calendar currentDate = Calendar.getInstance();

        /**
         * Selection string for GW2
         */
        final String gw2Selection = "meal_weekDay = "
                                        + (currentDate.get(Calendar.DAY_OF_WEEK) - 1)
                                        + " AND meal_place = "
                                        + Constants.GW2_ID;

        /**
         * Selection string for Mensa.
         */
        final String mensaSelection = "meal_weekDay = "
                                        + (currentDate.get(Calendar.DAY_OF_WEEK) - 1)
                                        + " AND meal_place = "
                                        + Constants.MENSA_ID;
        Cursor c;
        if (this.mItem.getTitle().equals("Mensa")) {
            c = this.mContext.getContentResolver().query(Meals.CONTENT_URI,
                                                         projection,
                                                         mensaSelection, null,
                                                         null);
        } else if (this.mItem.getTitle().equals("Cafeteria GW2")) {
            c = this.mContext.getContentResolver().query(Meals.CONTENT_URI,
                                                         projection,
                                                         gw2Selection, null,
                                                         null);
        } else {
            c = null;
        }
        this.setContentView(R.layout.meal_listview);
        // Turn of Progressbar.
        ProgressBar pB = (ProgressBar) this.findViewById(R.id.progressBar);
        pB.setVisibility(View.GONE);
        this.setTitle(this.mItem.getTitle());
        this.setCancelable(true);
        this.setCanceledOnTouchOutside(true);

        final ListView mealsListView = (ListView) this
                                        .findViewById(R.id.meal_listview);
        final String[] from = new String[] { Meals.MEAL_NAME, Meals.MEAL_DESC,
                Meals.MEAL_STUDENT_PRICE };
        final int[] to = new int[] { R.id.mealName, R.id.mealDesc,
                R.id.mealPrice };
        if (c != null) {
            mealsListView.setAdapter(new MensaCursorAdapter(this.mContext,
                                            R.layout.mensa_listview_item, c,
                                            from, to, 0));
            final TextView emptyTv = (TextView) this
                                            .findViewById(R.id.emptyTextView);
            emptyTv.setText("Heute gibt es hier kein Essen.");
            mealsListView.setEmptyView(emptyTv);
        } else {
            mealsListView.setVisibility(View.GONE);
            new InformationDialog(this.mContext, item);
            return;
        }

        this.show();
    }
}
